package com.liuceng.zen.web.controller;


import com.liuceng.zen.pojo.domain.CrtSizeMediumMenu;
import com.liuceng.zen.pojo.domain.FrontSelfOperateCreative;
import com.liuceng.zen.pojo.form.FrontSelfOperateCreativeForm;
import com.liuceng.zen.service.CrtSizeMediumMenuService;
import com.liuceng.zen.service.FrontSelfOperateCreativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yanwen on 16-11-3.
 * FrontSelfOperateCreativeController :
 */
@Controller
public class FrontSelfOperateCreativeController extends BaseController{
    /** 自运营广告创意*/
    @Autowired
    public FrontSelfOperateCreativeService frontSelfOperateCreativeService;
    @Autowired
    private CrtSizeMediumMenuService crtSizeMediumMenuService;
    // 广告创意/orig/ad
    @RequestMapping(value = "/front_creatives/index", method = RequestMethod.GET)
    public String adCreative(@ModelAttribute("form") FrontSelfOperateCreativeForm form) {
        form.setMemberId(getCurrentUserId());
        form.setCrtSizeMediumMenuId(form.getCrtSizeMediumMenuId()==null?0:form.getCrtSizeMediumMenuId());
        form.setPage(frontSelfOperateCreativeService.findPage(form));
        List<CrtSizeMediumMenu> crtSizeMediumMenus = crtSizeMediumMenuService.findAll(getCurrentUserId());
        form.setCrtSizeMediumMenus(crtSizeMediumMenus);
        return "frontSelfOperateCreatives/index";
    }

    // 获取添加页面
    @RequestMapping(value = "/ajax/front_creatives/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        List<CrtSizeMediumMenu> crtSizeMediumMenus = crtSizeMediumMenuService.findAll(getCurrentUserId());
        map.put("crtSizeMediumMenus",crtSizeMediumMenus);
        return "frontSelfOperateCreatives/_add";
    }

    // 获取新建和编辑广告添加创意页面
    @RequestMapping(value = "/ajax/front_creatives/add/{type}", method = RequestMethod.GET)
    public String add(ModelMap map, @PathVariable Integer type) {
        List<CrtSizeMediumMenu> crtSizeMediumMenus = crtSizeMediumMenuService.findSomeByType(getCurrentUserId(),type);
        map.put("crtSizeMediumMenus",crtSizeMediumMenus);
        return "ad/add_creative";
    }

    // 根据创意ID获取所有创意的详细情况
    @RequestMapping(value = "/ajax/get/front_creatives", method = RequestMethod.GET)
    @ResponseBody
    public List<FrontSelfOperateCreative> getCreatives(@ModelAttribute("form") FrontSelfOperateCreativeForm form) {
        List<FrontSelfOperateCreative> creatives = new ArrayList<>();
        if(form.getCreativesId()!=null&&form.getCreativesId().size()>0){
            creatives = frontSelfOperateCreativeService.findCreativeByIds(form.getCreativesId());
        }
        return creatives;
    }

    /**
     * 添加广告创意
     * @param frontSelfOperateCreative
     * @return
     */
    @RequestMapping(value = "/front_creatives/add", method = RequestMethod.POST)
    public @ResponseBody Object creativeAdd(FrontSelfOperateCreative frontSelfOperateCreative) {
        frontSelfOperateCreative.setMemberId(getCurrentUserId());
        frontSelfOperateCreative.setDepartmentId(getUserContext().getMember().getAdminDepartmentId());
        frontSelfOperateCreativeService.insert(frontSelfOperateCreative);
        return "success";
    }

    /**
     * 删除广告创意
     * @param frontSelfOperateCreative
     * @return
     */
    @RequestMapping(value = "/front_creatives/delete", method = RequestMethod.POST)
    public @ResponseBody Object creativeDelete(FrontSelfOperateCreative frontSelfOperateCreative) {
        Map<String,Object> map = new HashMap<>();
        map.put("frontSelfOperateCreativeId",frontSelfOperateCreative.getId());
        map.put("memberId",getCurrentUserId());
        FrontSelfOperateCreative creative=frontSelfOperateCreativeService.findOneByParams(map);
        if(creative==null) {
            frontSelfOperateCreativeService.delete(getCurrentUserId(), frontSelfOperateCreative.getId());
            return "success";
        }else{
            return "fails";
        }

    }

    // 广告创意/orig/ad
    @RequestMapping(value = "/ajax/front_creatives/select/{type}", method = RequestMethod.GET)
    public String adCreative(@ModelAttribute("form") FrontSelfOperateCreativeForm form, @PathVariable Integer type) {
        form.setMemberId(getCurrentUserId());
        form.setSitesetType(type);
        form.setCrtSizeMediumMenuId(form.getCrtSizeMediumMenuId()==null?0:form.getCrtSizeMediumMenuId());
        form.setPage(frontSelfOperateCreativeService.findPage(form));
        List<CrtSizeMediumMenu> crtSizeMediumMenus = crtSizeMediumMenuService.findSomeByType(getCurrentUserId(),type);
        form.setCrtSizeMediumMenus(crtSizeMediumMenus);
        form.setSitesetType(type);
        return "/ad/_creative";
    }
}
